﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.WebAdmin;
using ServiceWeb.WebAdmin;
using Microsoft.EntityFrameworkCore;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using ServiceWeb.DBBase;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class ProductLanguageCommonController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();


        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_product reqProduct, int PageIndex = 1, int PageSize = 15)
        {
            ViewData["reqProduct"] = reqProduct;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";


            if (!string.IsNullOrEmpty(reqProduct.p_guid))
            {
                sqlwhere = sqlwhere + "  and lp_p_guid = @p_guid ";
                sugarParameters.Add(new SugarParameter("p_guid", reqProduct.p_guid));
            }

            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and lp_c_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            string sql = "select *,(SELECT GROUP_CONCAT(l_name SEPARATOR ', ')  FROM bs_language\r\ninner join bs_language_iot_product_item on lpi_l_guid=l_guid where lpi_lp_guid=lp_guid) lname  from   bs_language_iot_product  where 1=1 " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "lp_id desc", ref totalRecord);

            ViewData["DataList"] = dt;
            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码

            return View(httpRequest);
        }


        public IActionResult listLanguage(IFormCollection FC, HttpReqModel httpRequest, iot_device reqDevice, iot_product reqProduct, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            ViewData["reqDevice"] = reqDevice;
            ViewData["reqProduct"] = reqProduct;
            ViewBag.d_gw_d_guid = httpRequest.gwdguid;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";

            if (!string.IsNullOrEmpty(reqProduct.p_guid))
            {
                sqlwhere = sqlwhere + "  and c_guid not in(select lp_c_guid from bs_language_iot_product where lp_p_guid = @p_guid) ";
                sugarParameters.Add(new SugarParameter("p_guid", reqProduct.p_guid));
            }

            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and c_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            string sql = "select *,(SELECT GROUP_CONCAT(l_name SEPARATOR ', ')  FROM bs_language\r\ninner join bs_language_config_item on ci_l_guid=l_guid where ci_c_guid=c_guid) lname from   bs_language_config   where 1=1 " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "c_id desc", ref totalRecord);
            ViewData["DataList"] = dt;
            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码

            return View(httpRequest);
        }



        public RespResultData addtoproduct(HttpReqModel httpRequest, string cguid, string pguid)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            bs_language_config config = eFIotBase.bs_language_config.Where(c => c.c_guid == cguid).FirstOrDefault();


            bs_language_iot_product product = new bs_language_iot_product();
            product.lp_p_guid = pguid;
            product.lp_c_guid = cguid;
            product.lp_add_time = DateTime.Now;
            product.lp_guid = Guid.NewGuid().ToString();
            product.lp_c_name = config.c_name;
            product.lp_c_code = config.c_code;
            eFIotBase.bs_language_iot_product.Add(product);
            eFIotBase.SaveChanges();

            List<bs_language_config_item> items = eFIotBase.bs_language_config_item.Where(c => c.ci_c_guid == cguid).ToList();

            foreach (bs_language_config_item item in items)
            {
                bs_language_iot_product_item additem = new bs_language_iot_product_item();
                additem.lpi_lp_guid = product.lp_guid;
                additem.lpi_c_guid = item.ci_c_guid;
                additem.lpi_ci_content = item.ci_content;
                additem.lpi_ci_addtime = DateTime.Now;
                additem.lpi_l_guid = item.ci_l_guid;
                additem.lpi_c_code = item.ci_c_code;
                additem.lpi_ci_guid = Guid.NewGuid().ToString();
                eFIotBase.bs_language_iot_product_item.Add(additem);
                eFIotBase.SaveChanges();
            }

            return RespResult.Sucess();
        }
        public RespResultData del(HttpReqModel httpRequest, int lpid)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            bs_language_iot_product product = eFIotBase.bs_language_iot_product.Where(c => c.lp_id == lpid).FirstOrDefault();
            eFIotBase.bs_language_iot_product.Where(c => c.lp_id == lpid).ExecuteDelete();
            eFIotBase.bs_language_iot_product_item.Where(c => c.lpi_lp_guid == product.lp_guid).ExecuteDelete();
            return RespResult.Sucess();
        }


    }
}